package com.itcast.yygh.user.controller;

import com.itcast.common.result.R;
import com.itcast.common.utils.AuthContextHolder;
import com.itcast.yygh.model.model.user.Patient;
import com.itcast.yygh.user.service.PatientApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 就诊人管理功能
 */
@Api("就诊人管理")
@RestController
@RequestMapping("/api/userinfo/patient")
public class PatientApiController {

    @Resource
    private PatientApiService patientApiService;

    //根据用户id查询所有的就诊人信息
    @ApiOperation("根据登录的用户id查询所有就诊人信息")
    @GetMapping("/auth/findAll")
    public R findAll(HttpServletRequest request){
        //1.先获取请求中的userid
        Long userId = AuthContextHolder.getUserId(request);
        //2.根据用户id查询就诊人信息
        List<Patient> patients = patientApiService.findAllByUserId(userId);
        //3.返回查询到的数据
        return R.ok().data("list",patients);
    }
    //保存就诊人信息
    @ApiOperation("保存就诊人信息")
    @PostMapping("/auth/save")
    public R save(@RequestBody Patient patient,HttpServletRequest request){
        //1.先获取登录用户的id
        Long userId = AuthContextHolder.getUserId(request);
        //2.需要把用户id设置到就诊人对象中
        patient.setUserId(userId);
        try{
            //3.保存就诊人信息
            patientApiService.save(patient);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }
    //查询单个就诊人信息
    @ApiOperation("查询单个就诊人数据")
    @GetMapping("/auth/get/{id}")
    public R get(@PathVariable("id") Long id){
        //根据id查询数据
        Patient patient = patientApiService.getById(id);
        //返回查询后的结果
        return R.ok().data("patient",patient);
    }
    //修改就诊人信息
    @ApiOperation("修改就诊人信息")
    @PostMapping("/auth/update")
    public R update(@RequestBody Patient patient){
        try{
            patientApiService.updateById(patient);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }
    //删除就诊人信息
    @ApiOperation("删除就诊人信息")
    @GetMapping("/auth/remove/{id}")
    public R remove(@PathVariable Long id){
        try{
            //调用service中的方法
            patientApiService.removeById(id);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }

    }
    @ApiOperation(value = "获取就诊人")
    @GetMapping("inner/get/{id}")
    public Patient getPatientOrder(
            @ApiParam(name = "id", value = "就诊人id", required = true)
            @PathVariable("id") Long id) {
        return patientApiService.getById(id);
    }
}
